// ==UserScript==
// @name         京东校时抢券
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  try to take over the world!
// @author       You
// @match        https://credit.jd.com/channel/coupon.html?couponBusinessId=*&actId=*&agreementSource=*
// @grant        none
// ==/UserScript==


// 使用方法：
// window.JDC.start('2018-06-21 00:41:00', 'User.grabCoupon(true)');
// 此页面 第二个参数不用改，第一个参数改成抢券的准确时间。

// 注意 alert confirm等 会阻塞时间的正常计算。
var JDTime = {
    init: function(){
        this.time = (new Date()).getTime();
        this.setItv();
        this.correctTime();
        setInterval(() => {
            this.correctTime();
        }, 60000)
    },
    setItv: function () {
        clearInterval(this.itv);
            this.itv = setInterval(() => {
                this.time += 1000;
                console.log(this.getDate());
            }, 1000);
    },
    getServerTime: function(){
        return (new Date($.ajax({async: false}).getResponseHeader("Date"))).getTime();
    },
    getTime: function() {
        return this.time;
    },
    getDate: function() {
        return new Date(this.time);
    },
    correctTime: function() {
        console.log('开始校时');
        let tmpTime = this.getServerTime();
        let newTime = null;
        let ping = null;
        let tmpTime1 = null;
        for(var i = 0; i < 9999999999; i ++) {
            tmpTime1 = (new Date()).getTime();
            newTime = this.getServerTime();
            ping = (new Date()).getTime() - tmpTime1;
            console.log(ping);
            if(newTime <= tmpTime) {
                continue;
            } else {
                this.time = newTime - parseInt(ping / 2);
                this.setItv();
                break;
            }
        }
        console.log('完成校时');
    }
}
JDTime.init();


var JDC = null;
$(function() {
    'use strict';
    var JDC = {
        bidTime: null,
        funcStr: null,
        tm2: null,  //倒计时定时器
        start: function(timeStr, fn) {
            this.bidTime = (new Date(timeStr)) . getTime();
            this.funcStr = fn;
            this.logFunc('开始..');
            this.bidTm();
        },
        bidTm: function(){
            var _this = this;
            var now = JDTime.getTime();
            clearTimeout(this.tm2);
            var tmm = this.bidTime - now;
            if (tmm < 0) {
                _this.logFunc('时间已过');
            } else {
                _this.logFunc(tmm + 'ms 后开抢');
                this.tm2 = setTimeout(function(){
                    _this.bidFunc();
                },this.bidTime - now);
            }
        },
        bidFunc: function(){
            if(this.funcStr){
                eval(this.funcStr);
            } else {
                this.logFunc('未传入函数');
            }
        },
        logFunc: function(info){
            var d = JDTime.getDate();
            var time = d.getHours()+":"+d.getMinutes()+":"+d.getSeconds()+"."+d.getMilliseconds();
            console.log("["+time+"]"+info);
        },
    };
    window.JDC = JDC;
});
